<template>
	<div class="homework w944">
		<alert ref="alert"></alert>
		<div class="label_top">
			<button @click="cancel"><i><&nbsp;</i>返回</button>
			<p>{{updateTitle}}</p>
		</div>
		<div class="homeworkLine">
			<span class="tit">作业名称：</span>
			<input type="text" v-model="name"/>
		</div>
		<div class="homeworkLine heiT">
			<span class="tit">作业类型：</span>
			<div class="homeworkR">
				<el-radio-group v-model="homeworkType">
					<el-radio label="1-1" name="Type">视频作业—学员拍摄或上传视频</el-radio>
					<el-radio label="1-2" name="Type">文件作业—学员上传文件</el-radio>
				</el-radio-group>
				<em>文件支持ppt、pptx、xls、doc、docx、pdf格式</em>
			</div>
		</div>
		<div class="homeworkLine">
			<span class="tit">作业要求：</span>
			<vue-ueditor-wrap v-model="homeworkRequire" :config="myConfig" style="margin: 14px 0 0 85px;"></vue-ueditor-wrap>
		</div>
		<div class="footerButton">
			<a @click="checkSave" v-bind:style="{background: clickLock==true ? '#44CEF6' : '#3a8ee6'}">保存</a>
			<a @click="cancel" class="cancel">取消</a>
		</div>
	</div>
</template>

<script>
	import alert from '../../components/Alert.vue';
	export default{
		data(){
			return{
				clickLock:true,
				updateTitle:'添加作业',
				updateTitleCourse:'',
				teacherTitle:'',
				lecturerid:'',
				nowType:'add',
				liveId:'',
				id:'',
				name:'',
				homeworkType:'',
				homeworkRequire:'',
				homeworkIsBm:'',
				homeworkIsBx:'',
				homeworkIsCj:'',
				myConfig: {
					autoHeightEnabled: false,
					initialFrameHeight: 420,
					initialFrameWidth: '840'
				}
			}
		},
		methods:{
			getParams() {
				let that = this;
				var routerParams = this.$route.params.type;
				this.liveId = this.$route.params.liveId;
				this.updateTitleCourse = this.$route.params.updateTitleCourse;
				this.pathKey = this.$route.params.pathKey;
				this.teacherTitle = this.$route.params.teacherTitle;
				this.lecturerid = this.$route.params.lecturerid;
				if(routerParams == 'edit') {
					that.nowType = 'edit';
					that.updateTitle = "编辑作业";
				}else{
					that.nowType = 'add';
				}
				console.log('homework',this.$route.params);
			},
			add_type(){
				let that = this;
				let s= {
					courseid:that.liveId,
					name:that.name,
					homeworkType:that.homeworkType,
					homeworkRequire:that.homeworkRequire,
					homeworkIsBm:0,
					homeworkIsBx:0,
					homeworkIsCj:1,
					rootcompanyid:1
				};
				if(this.clickLock==true){
					this.clickLock = false;
					this.$ajax({
						url: this.$root.jifuUrl + '/mns/coursewareHomework/addCoursewareHomework',
						headers:{'ContentType': 'application/json; charset=utf-8'},
						data: s,					
						method: 'POST',
						success: res => {
							if(res.data.code == 200) {
								this.$refs.alert.show({alertText: '保存成功'});
								setTimeout(() =>{
									this.$router.push({
										name: 'courseware',
										params: {
											liveId:this.liveId,
											updateTitle:this.updateTitleCourse,
											pathKey:this.pathKey,
											teacherTitle:this.teacherTitle,
											lecturerid:this.lecturerid
										}
									})
								},2000)
							}else{
								this.clickLock = true;
								this.failText = res.data.message;
								this.$rootFunction().deleteResFail();
							}
						}
					})
				}
			},
			edit_type(){
				let that = this;
				let s= {
					id:that.id,
					courseid:that.liveId,
					name:that.name,
					homeworkType:that.homeworkType,
					homeworkRequire:that.homeworkRequire,
					homeworkIsBm:0,
					homeworkIsBx:0,
					homeworkIsCj:1,
					rootcompanyid:1
				};
				this.$ajax({
					url: this.$root.jifuUrl + '/mns/coursewareHomework/updateCoursewareHomework',
					headers:{'ContentType': 'application/json; charset=utf-8'},
					data: s,					
					method: 'POST',
					success: res => {
						if(res.data.code == 200) {
							this.$refs.alert.show({alertText: '保存成功'});
							setTimeout(() =>{
								this.$router.push({
									name:'courseware',
									params: {
										liveId:this.liveId,
										updateTitle:this.updateTitleCourse,
										pathKey:this.pathKey,
										teacherTitle:this.teacherTitle,
										lecturerid:this.lecturerid
									}
								})
							},2000)
						}else{
							this.failText = res.data.message;
							this.$rootFunction().deleteResFail();
						}
					}
				});
			},
			checkSave(){
				let that = this;
				if(that.nowType == 'add') {
					that.add_type();
				}else if(that.nowType == 'edit') {
					this.edit_type();
				}
			},
			cancel() {
				this.$router.push({
					name: 'courseware',
					params: {
						liveId:this.liveId,
						updateTitle:this.updateTitleCourse,
						teacherTitle:this.teacherTitle,
						lecturerid:this.lecturerid,
						pathKey:this.pathKey
					}
				})
			}
		},
		components:{
			alert
		},
		mounted(){
			this.getParams();
			if(this.liveId==null||this.updateTitle==null){
				this.$store.commit('GET_homework');
				var s =this.$store.state.homework;
				this.liveId=s.liveId;
				this.updateTitleCourse = s.updateTitleCourse;
				this.teacherTitle = s.teacherTitle;
				this.lecturerid = s.lecturerid;
				this.pathKey = s.pathKey
			};
			if(this.nowType=="edit"){
				console.log('作业--306',this.nowType,this.$root.homeworkMsg);
				this.id = this.$root.homeworkMsg.id;
				this.courseid = this.$root.homeworkMsg.courseid;
				this.name = this.$root.homeworkMsg.name;
				this.homeworkType = this.$root.homeworkMsg.homeworkType;
				this.homeworkRequire = this.$root.homeworkMsg.homeworkRequire;
				this.homeworkIsBm = this.$root.homeworkMsg.homeworkIsBm;
				this.homeworkIsBx = this.$root.homeworkMsg.homeworkIsBx;
				this.homeworkIsCj = this.$root.homeworkMsg.homeworkIsCj;
				this.rootcompanyid = this.$root.homeworkMsg.rootcompanyid;
			}
			window.addEventListener("beforeunload",()=>{
				s={
					liveId : this.liveId,
					updateTitleCourse:this.updateTitleCourse,
					teacherTitle:this.teacherTitle,
					lecturerid:this.lecturerid,
					pathKey:this.pathKey
				}	
				this.$store.commit('SET_homework', s);
			});
		},
		watch:{
			$route: 'getParams'
    	}
	}
</script>

<style>
	.homeworkLine{margin-bottom: 20px;position: relative;}
	.homeworkLine.heiT{height: 60px;}
	.homeworkLine span{color: #333;}
	.homeworkLine .tit{font-size: 16px;color: #333;width: 80px;text-align: right;display: inline-block;}
	.homeworkLine input{width: 200px;height: 30px;line-height:30px;border: 1px solid #ccc;padding-left: 10px;color: #666;font-size: 14px;border-radius: 5px;}
	.homeworkLine.heiT .el-radio{display: block;margin-bottom: 5px;}
	.homeworkLine .homeworkR{position: absolute;top: 0;left: 84px;}
	.homeworkLine em{display: block;font-size: 14px;color: #999;font-style: normal;}
	.homeworkLine .el-select,.homeworkLine .el-select input{width: 212px;height: 32px;line-height: 32px;}
	.homeworkLine .el-select .el-select__caret{height: 32px;line-height: 32px;}
</style>